package front_article_api

import (
	"blog_go/global"
	"blog_go/models"
	"blog_go/models/res"
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
)

type ArticleCalendarRsp struct {
	Calendar string `json:"calendar"`
	Count    uint   `json:"count"`
}

func (ArticleApi) ArticleCalendar(c *gin.Context) {
	var articleModelList []models.ArticleModel
	err := global.DB.Select("created_at").Find(&articleModelList).Error
	if err != nil {
		logrus.Error(err)
		panic(err)
	}
	dataMap := make(map[string]uint)
	for _, model := range articleModelList {
		createdAtStr := model.CreatedAt.Format("2006-01-02")
		dataMap[createdAtStr] = dataMap[createdAtStr] + 1
	}
	var articleCalendarResponseList []ArticleCalendarRsp
	for k, v := range dataMap {
		articleCalendarResponseList = append(articleCalendarResponseList, ArticleCalendarRsp{k, v})
	}
	res.OkWithDate(articleCalendarResponseList, c)
}
